package com.of.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.of.mapper.ItemCatMapper;
import com.of.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private ItemCatMapper itemCatMapper;

    /*
     * 思路
     * 1.判断map集合中是否存在key
     * 2.如果存在key，则拿到key的值
     * */
    public Map<Integer, List<ItemCat>> getMap() {
        Map<Integer, List<ItemCat>> map = new HashMap<>();
        //查询数据库的所有记录
        List<ItemCat> list = itemCatMapper.selectList(null);
        for (ItemCat itemCat : list) {
            int key = itemCat.getParentId();
            /*判断  防止数据被覆盖，也就是说不同的子集他有相同的父级，*/
            if (map.containsKey(key)) {/*true 有数据*/
                map.get(key).add(itemCat);
            } else {/*false  没有数据*/
                List<ItemCat> childrenList = new ArrayList<>();
                childrenList.add(itemCat);
                map.put(key, childrenList);
            }
        }
        return map;
    }

    /**
     * 只查询一级菜单  parent_id=0 或者 level=1
     * 查询二级菜单信息 parent_id = 一级ID
     * 查询三级菜单信息 parent_id = 二级ID
     * 思考: 如何优化代码   要求查询时间最好 控制在20ms+-
     *
     * @param level
     * @return
     */
    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        //long startTime=System.currentTimeMillis();
        //1.获取数据信息
        Map<Integer, List<ItemCat>> map = getMap();
        if (level == 1) {/*只获取一级菜单数据*/
            return map.get(0);
        }
        if (level == 2) {
            return getTowList(map);
        }
        /*获取1-2-3级数据*/
        //long endTime=System.currentTimeMillis();
        //System.out.println("耗时:"+(endTime-startTime));
        return getThreeList(map);
    }


    private List<ItemCat> getThreeList(Map<Integer, List<ItemCat>> map) {
//        /*获取一级和二级数据*/
//        List<ItemCat> oneList = getTowList(map);
//        for (ItemCat oneItemCat : oneList) {
//            List<ItemCat> twoList = oneItemCat.getChildren();
//            if (twoList == null) {
//                /*说明 该一级下没有二级数据 所以跳过本次循环 进入下一次循环*/
//                continue;
//            }
//            for (ItemCat twoItemCat : twoList) {
//                int key = twoItemCat.getId();
//                List<ItemCat> threeList = map.get(key);
//                twoItemCat.setChildren(threeList);
//            }
//        }
//        return oneList;
        /*获取一级和二级数据*/
        List<ItemCat> oneList = getTowList(map);
        oneList.forEach(oneItemCat -> {
            List<ItemCat> twoList = oneItemCat.getChildren();
            if (twoList != null) {
                twoList.forEach(twoItemCat -> {
                    int key = twoItemCat.getId();
                    List<ItemCat> threeList = map.get(key);
                    twoItemCat.setChildren(threeList);
                });
            }
        });
        return oneList;
    }

    /*获取1-2*/
    private List<ItemCat> getTowList(Map<Integer, List<ItemCat>> map) {
//        /*获取一级数据*/
//        List<ItemCat> oneList = map.get(0);
//        for (ItemCat oneItemCat : oneList) {
//            /*根据一级ID查询二级集合*/
//            int key = oneItemCat.getId(); //id map中的key
//            List<ItemCat> twoList = map.get(key);
//            /*将二级数据封装到一级中*/
//            oneItemCat.setChildren(twoList);
//        }
//        return oneList;
        /*获取一级数据*/
        List<ItemCat> oneList = map.get(0);
        oneList.forEach(oneItemCat -> {
            /*根据一级ID查询二级集合*/
            int key = oneItemCat.getId(); //id map中的key
            List<ItemCat> twoList = map.get(key);
            /*将二级数据封装到一级中*/
            oneItemCat.setChildren(twoList);
        });
        return oneList;
    }


    @Override
    @Transactional
    public void saveItemCat(ItemCat itemCat) {
        itemCat.setStatus(true);
                /*.setCreated(new Date())
                .setUpdated(itemCat.getCreated());*/
        itemCatMapper.insert(itemCat);
    }

    /*
     * 判断是否为三级，如果是则直接删除
     * 判断是否为二级，如果是二级，先删除三级，再删除二级
     * */
    @Override
    @Transactional
    public void deleteItemCat(ItemCat itemCat) {
        if (itemCat.getLevel() == 3) {
            itemCatMapper.deleteById(itemCat.getId());
            return;
        }
        if (itemCat.getLevel() == 2) {
            int twoId = itemCat.getId();
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", twoId);
            /*删除的是三级数据*/
            itemCatMapper.delete(queryWrapper);
            itemCatMapper.deleteById(twoId);

        }
        /*执行到这里 只剩下 一级菜单
         * 1.应该先查询二级id
         * */
        int oneId = itemCat.getId();
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", oneId);
        List twoIdList = itemCatMapper.selectObjs(queryWrapper);
        if (twoIdList.size() == 0) {
            /*如果没有二级数据。则直接删除一级信息*/
            itemCatMapper.deleteById(oneId);
        } else {
            /*表示有二级数据 可以删除三级*/
            /*如何让快速删除三级 二级 一级*/
            queryWrapper.clear();
            queryWrapper.in("parent_id", twoIdList)
                    .or()
                    .in("id", twoIdList)
                    .or()
                    .eq("id", oneId);
            itemCatMapper.delete(queryWrapper);
        }
    }

    @Override
    @Transactional
    public void updateStatus(ItemCat itemCat) {
        itemCat.setUpdated(new Date()); //设定当前时间
        itemCatMapper.updateById(itemCat);
    }

    @Override
    @Transactional
    public void updateItemCat(ItemCat itemCat) {
        itemCat.setUpdated(new Date());
        UpdateWrapper<ItemCat> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", itemCat.getId());
        itemCatMapper.update(itemCat, updateWrapper);
        //itemCatMapper.updateItemCat(itemCat);
    }
}




















  /*  @Override
    public List<ItemCat> findItemCatList(Integer level) {
        long startTime=System.currentTimeMillis();
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",0);
        List<ItemCat> oneList = itemCatMapper.selectList(queryWrapper);
        for (ItemCat oneItemCat : oneList){
            //获取一级对象的ID
            int oneId = oneItemCat.getId();
            //清空多余的条件
            queryWrapper.clear();
            //查询二级数据
            queryWrapper.eq("parent_id",oneId);
            List<ItemCat> twoList = itemCatMapper.selectList(queryWrapper);
            for(ItemCat twoItemCat:twoList){
                int twoId=twoItemCat.getId();
                queryWrapper.clear();
                queryWrapper.eq("parent_id",twoId );
                List<ItemCat> threeList = itemCatMapper.selectList(queryWrapper);
                twoItemCat.setChildren(threeList);
            }
            //将二级封装到一级对象中
            oneItemCat.setChildren(twoList);
        }
        long endTime=System.currentTimeMillis();
        System.out.println("总耗时:"+(endTime-startTime));
        return oneList;
    }*/